#!/usr/bin/env python
# coding=utf-8

from xml.dom import  minidom, Node
from xml.dom.minicompat import NodeList
import sys

#获取下面一层子节点的tag为name的节点
def getElementsByTagNameForChild(parent, name) :
    rc = NodeList()
    for node in parent.childNodes:
        if node.nodeType == Node.ELEMENT_NODE and \
            (name == "*" or node.tagName == name):
            rc.append(node)
    return rc

def parseAppXML():
    filename=sys.argv[1]
    #filename=filePath
    doc=minidom.parse(filename)
    root=doc.documentElement
    packageName=root.getAttribute("package")
    print(packageName)

    applicationNodes=getElementsByTagNameForChild(root,"application")
    metaNodes = getElementsByTagNameForChild(applicationNodes[0], "meta-data")
    activityNodes = getElementsByTagNameForChild(applicationNodes[0], "activity")
    serviceNodes = getElementsByTagNameForChild(applicationNodes[0], "service")
    receiverNodes = getElementsByTagNameForChild(applicationNodes[0], "receiver")
    providerNodes= getElementsByTagNameForChild(applicationNodes[0],"provider")
    usesPermissionNodes = getElementsByTagNameForChild(root, "uses-permission")

    if (applicationNodes.length > 1) :
        print('Error: The num of applications is more than 1')
        exit(-1)
    ACCESS_ID = "None"
    ACCESS_KEY = "None"

    for node in metaNodes:
        if (node.getAttribute("android:name") == "XG_V2_ACCESS_ID"):
            if (ACCESS_ID != "None"):
                ACCESS_ID = "Error, more tan one ACCESS_ID"
            else:
                ACCESS_ID = node.getAttribute("android:value")
        if (node.getAttribute("android:name") == "XG_V2_ACCESS_KEY"):
            if (ACCESS_KEY != "None"):
                ACCESS_KEY = "Error, more tan one ACCESS_KEY"
            else:
                ACCESS_KEY = node.getAttribute("android:value")
    print('ACCESS_ID: '+ACCESS_ID)
    print('ACCESS_KEY: '+ACCESS_KEY)

    checkStatus = {
        'XGPushActivity': 'Not Exist',
        'XGPushServiceV3': 'Not Exist',
        'XGDaemonService': 'Not Exist',
        'XGPushReceiver': 'Not Exist',
        'UsesPermission': 'Not Exist',
        'XGPushProvider':'Not Exist',
        'XGContentProvider':'Not Exits',
        'XGMidProvider':'Not Exits'
    }

    '''------checking activity------'''
    for node in activityNodes:
        if(node.getAttribute("android:name")=="com.tencent.android.tpush.XGPushActivity"):
            checkStatus['XGPushActivity'] = 'OK'

    '''------checking receiver------'''
    for node in receiverNodes:
        if(node.getAttribute("android:name")=="com.tencent.android.tpush.XGPushReceiver"):
            checkStatus['XGPushReceiver'] = ""
            if (node.getAttribute("android:process") == ""):
                checkStatus['XGPushReceiver'] += 'android:process not set; '
            actionStatus = {
                "com.tencent.android.tpush.action.SDK": "not set",
                "com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE": "not set",
                "android.intent.action.USER_PRESENT": "not set",
                "android.net.conn.CONNECTIVITY_CHANGE": "not set"
            }
            for intentFilterNode in getElementsByTagNameForChild(node, "intent-filter"):
                for actionNode in getElementsByTagNameForChild(intentFilterNode, "action"):
                    for key in actionStatus.keys():
                        if (actionNode.getAttribute("android:name") == key):
                            actionStatus[key] = "OK"
            for key in actionStatus.keys():
                if (actionStatus[key] != "OK"):
                    checkStatus['XGPushReceiver'] += (key + " " + actionStatus[key] + "; ")

            if (checkStatus['XGPushReceiver'] == ""):
                checkStatus['XGPushReceiver'] = "OK"

    '''------checking service------'''
    for node in serviceNodes :
        if (node.getAttribute("android:name") == "com.tencent.android.tpush.service.XGPushServiceV3") :
            checkStatus['XGPushServiceV3'] = ""
            if (node.getAttribute("android:process") == ""):
                checkStatus['XGPushServiceV3'] += 'android:process not set; '
            if (node.getAttribute("android:exported") != "true"):
                checkStatus['XGPushServiceV3'] += 'exported is not set to true; '
            if (node.getAttribute("android:persistent") != "true"):
                checkStatus['XGPushServiceV3'] += 'persistent is not set to true; '
            if (checkStatus['XGPushServiceV3'] == "") :
                checkStatus['XGPushServiceV3'] = "OK"
        elif (node.getAttribute("android:name") == "com.tencent.android.tpush.service.XGDaemonService") :
            checkStatus['XGDaemonService'] = ""
            if (node.getAttribute("android:process") == ""):
                checkStatus['XGDaemonService'] += 'android:process not set; '
            if (checkStatus['XGDaemonService'] == "") :
                checkStatus['XGDaemonService'] = "OK"

    '''------checking provider------'''
    for node in providerNodes :
        if(node.getAttribute("android:name")=="com.tencent.android.tpush.SettingsContentProvider"):
            checkStatus['XGContentProvider']=""
            if(node.getAttribute("android:authorities") != packageName+'.TPUSH_PROVIDER'):
                checkStatus['XGContentProvider']+= 'authorities is not set right; '
            if(node.getAttribute("android:exported") != "false"):
                checkStatus['XGContentProvider']+='exported is not set to false'
            if(checkStatus['XGContentProvider'] == ""):
                checkStatus['XGContentProvider']="OK"
        elif (node.getAttribute("android:name")=="com.tencent.android.tpush.XGPushProvider"):
            checkStatus['XGPushProvider']=""
            if (node.getAttribute("android:authorities") != packageName + '.AUTH_XGPUSH'):
                checkStatus['XGPushProvider'] += 'authorities is not set right; '
            if (node.getAttribute("android:exported") != "true"):
                checkStatus['XGPushProvider'] += 'exported is not set to true'
            if (checkStatus['XGPushProvider'] == ""):
                checkStatus['XGPushProvider'] = "OK"
        elif (node.getAttribute("android:name")=="com.tencent.mid.api.MidProvider"):
            checkStatus['XGMidProvider']=""
            if (node.getAttribute("android:authorities") != packageName + '.TENCENT.MID.V3'):
                checkStatus['XGMidProvider'] += 'authorities is not set right; '
            if (node.getAttribute("android:exported") != "true"):
                checkStatus['XGMidProvider'] += 'exported is not set to true'
            if (checkStatus['XGMidProvider'] == ""):
                checkStatus['XGMidProvider'] = "OK"

    '''------checking UsesPermission------'''
    usesPermissionStatus = {
        "android.permission.INTERNET": "not set",
        "android.permission.READ_PHONE_STATE": "not set",
        "android.permission.ACCESS_WIFI_STATE": "not set",
        "android.permission.ACCESS_NETWORK_STATE": "not set",
        "android.permission.RECEIVE_BOOT_COMPLETED": "not set",
        "android.permission.RESTART_PACKAGES": "not set",
        "android.permission.BROADCAST_STICKY": "not set",
        "android.permission.RECEIVE_USER_PRESENT": "not set",
        "android.permission.WRITE_EXTERNAL_STORAGE": "not set",
        "android.permission.WAKE_LOCK": "not set",
        "android.permission.KILL_BACKGROUND_PROCESSES": "not set",
        "android.permission.GET_TASKS": "not set",
        "android.permission.READ_LOGS": "not set",
        "android.permission.VIBRATE": "not set"
    }
    for node in usesPermissionNodes:
        for key in usesPermissionStatus.keys():
            if (node.getAttribute("android:name") == key):
                usesPermissionStatus[key] = "OK"
    checkStatus['UsesPermission'] = ""
    for key in usesPermissionStatus.keys():
        if (usesPermissionStatus[key] != "OK"):
            checkStatus['UsesPermission'] += (key + " " + usesPermissionStatus[key] + "; ")
    if (checkStatus['UsesPermission'] == ""):
        checkStatus['UsesPermission'] = "OK"

    for key in checkStatus.keys():
        print(key+": "+checkStatus[key])

if __name__ == '__main__':
    parseAppXML()




